#ifndef __H_NODE_SERVER_H__
#define __H_NODE_SERVER_H__

#include <deque>
#include <vector>
#include <map>
#include "ICC_Timer_Handler.h"
#include "RedisClient.h"
#include "Logic.h"
#include "GameBase.h"
#include "baseDbPart.h"
#include "CClient.h"
#include "HostBase.h"
#include "globalDefine.h"
#include "signalevent.h"

enum 
{
	NODE_EV_SET_TYPE = 0x100,
	NODE_EV_SET_CACHE = 0x101, 
	NODE_EV_SET_SVR_REG_PARAM = 0x102,
	NODE_EV_SET_SUBSCRIBE_CMD = 0x103,
};

class CNodeServer :public CGameBase, BYTimerOject
{
public:
	CNodeServer();
	virtual ~CNodeServer();
public:
	// interface
	virtual int ProcEvent(int ev_id, SpiderEvent* pEvent = NULL);
	RedisClient* GetCacheClient(const std::string& name, const int index = 0);
	CBaseDbPart* GetDBHandler(const std::string& name, const int index = 0);
	int SendPacket(CCPacketBase* pPkg, const unsigned short dsttype, const unsigned int dstid = 0);
	int SendPacketToService(CCPacketBase* pPkg, int type, int mod_num);

	int SendtoSrv(CCPacketBase* pPkg, const unsigned short dsttype, const unsigned int mod_uid = 0);

	int BroadcastMessageDirect(CCPacketBase* pPkg, const unsigned short dst_type);

	int InitMtSend(void);
	// callback 
	virtual bool InitGame();
	virtual int OnBYTimeOut(int Timerid);
	virtual bool ReadCommonConf(const char* strxml);
	virtual int OnClose(CClientHandler* pHandler);
	virtual int ProcessPacket(CClientHandler * pHandler, NETInputPacket* inputPkg);
	virtual int OnBackClose(ClientBase* pBackClient);
	virtual int ProccessBackPacket(ClientBase* pBackClient, NETInputPacket* inputPkg);
	virtual int OnBackConnected(ClientBase* pBackClient);
	virtual int OnEventCallBack(stEventItem* pitem);

private:
	int ProcLogFileTimer();
	int ProcHeatBeatTimer();
// 	struct stPushCache
// 	{
// 		time_t m_time;
// 		CCPacketBase* m_pPack;
// 		stPushCache()
// 		{
// 			m_time = 0;
// 			m_pPack = NULL;
// 		}
// 	};
// 	map<unsigned int, stPushCache> m_pushCache;
	unsigned int genSeq(void);
	bool m_initFlag;
public:
	int ProcServerRegister(NETInputPacket* inputPkg, CClientHandler* pHandler);
	int ProcServerClose(CClientHandler* pHandler);
	int ProcRecvCacheConf(ClientBase* pBackClient, NETInputPacket* inputPkg);
	int ProcPushNewConn(NETInputPacket* inputPkg, ClientBase* pBackClient);
	int ProcPublishService(NETInputPacket* inputPkg, ClientBase* pBackClient);
	int ProcOnlineQueryRsp(NETInputPacket* inputPkg, CCSocketHandler* handler);
	int ProcSetLogLevel(NETInputPacket* inputPkg, CCSocketHandler* handler);
public:
	int ReqCacheConf(ClientBase* pBackClient);
	int RegisterToMaster(ClientBase* pBackClient);
	int RegisterToServer(ClientBase* pBackClient);
	bool ConnectToCache();
	int  SendPacketToSrv(CCPacketBase* pPkg, const unsigned short dsttype, const unsigned int dstid = 0);
	bool SendPacketFromBack(CCPacketBase* pPkg, const unsigned short dsttype, const unsigned int dstid = 0);
	bool SendPacketFromClient(CCPacketBase* pPkg, const unsigned short dsttype, const unsigned int dstid = 0);
	bool SendPacketFromDispatch(CCPacketBase* pPkg, const unsigned short dsttype, const unsigned int dstid = 0);

	CacheConf* CreateCacheConf(std::string name);
	DBConf* CreateDBConf(std::string name);
	CacheConf* GetCahceConf(std::string name);
	DBConf* GetDBConf(std::string name);

	RedisClient* CreateCacheClinet(const std::string& name, const int index = 0);
	CBaseDbPart* CreateDBHandler(const std::string& name, const int index = 0);

	int AddServicePkgList(CCPacketBase* pPkg, int type, int mod_num);
	int AddSrvPkgList(CCPacketBase* pPkg, int type, int svid);
	int SendPkgQueue();
	int PushPlayerMsg(unsigned int uid, CCPacketBase* pPkg);
public:
	SrvTypeList  m_svrTypeList;	// 作为服务端接受其它服务连接
	BackTypeList m_backTypeList; // 作为客户端连其它服务

	ServerConf masterConf;
	CClient* m_pMaster;

	ServiceConfMap m_serviceConfList;
	ServiceTypeList m_serviceTypeList;
	PacketBufferList m_servicePkgList;
	PacketBufferList m_srvPkgList;    

	vector<CONFITEM> conf_item;
	SvrRegParam svrRegParam;

	CacheConfList  m_cacheConfList;
	RedisClientList m_redisClientList;
	std::map<std::string, std::map<int, RedisClient*> > m_redisClientCopy;

	DBConfList  m_dbConfList;
	DBClientList m_dbClientList;
	std::map<std::string, std::map<int, CBaseDbPart*> > m_dbClientCopy;

	std::vector<int>  m_subscribeCmds;

	CHostBase* m_pHost;
	int m_type;

	CSignalEvent* m_pSignalEvent;
};


#endif


